import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @Author Felix Torn
 * @create: 2024-06-08 19:25
 **/
public class Utils {
    public static List<List<String>> getDividedLines(String filePath, int n) throws IOException {
        // 读取文件中的所有行
        List<String> lines = Files.readAllLines(Paths.get(filePath));
        if (lines.isEmpty()) {
            System.out.println("文件为空或读取失败");
        }
        // 分割行并存入ArrayList
        return divideLines(lines, n);
    }

    public static List<List<String>> divideLines(List<String> lines, int n) {
        List<List<String>> dividedLists = new ArrayList<>();
        int totalLines = lines.size();
        int baseSize = totalLines / n;
        int remainder = totalLines % n;

        int start = 0;
        for (int i = 0; i < n; i++) {
            int end = start + baseSize + (i < remainder ? 1 : 0);
            dividedLists.add(new ArrayList<>(lines.subList(start, end)));
            start = end;
        }

        return dividedLists;
    }
}
